#include "ListControl.h"
#include "IM_CoreApi.h"
#include <QDateTime>
#include <QDateTime>
#include <QDateTime>
#include <QDebug>
#include <QQmlContext>
#include "Control/Init.h"
using namespace IM_GUI_KIT ;
void ListControl::deleteFriend(QString friendid,int type)
{
  IM_CoreApi::Instance()->getListControl()->deleteFriend(friendid,type);
  emit  friendSize( IM_CoreApi::Instance()->getListControl()->getFriendListPort()->size());

}

void ListControl::deleteSession(QString sessionid,int type)
{
  IM_CoreApi::Instance()->getListControl()->deleteSession(sessionid,type);
  emit  sessionSize( IM_CoreApi::Instance()->getListControl()->getSessionListPort()->size());
   Init::Instance()->getEngine()->rootContext()->setContextProperty("currSessionList",QVariant::fromValue(nullptr));
}

void ListControl::createSession(QString sessionid,int type)
{
  IM_CoreApi::Instance()->getListControl()->createSession(sessionid,type);
  emit  sessionSize( IM_CoreApi::Instance()->getListControl()->getSessionListPort()->size());

}

void ListControl::sendMessageByText(QString sessionid, int type, QString body)
{
  //判断是否可以发送
  if(IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->count(sessionid)>0){

      // 执行发送操作
      //1 将 消息 拼接到 列表
      QDateTime time = QDateTime::currentDateTime();   //获取当前时间
      int timeT = time.toTime_t();   //将当前时间转为时间戳
      int64_t ts=timeT;
      int msec=time.time().msec();
      ts=ts*1000+msec;
      UIMessageChatModel *message=new UIMessageChatModel(sessionid,"aaa","qrc:/other/icon/qqimg.jpg",ts,"dad",2);
      IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->find(sessionid).value()->addMessageToList(message);
      // 更新
      Init::Instance()->getEngine()->rootContext()->setContextProperty("currSessionList",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->find(sessionid).value()->getMessageList()));
    }
  else {
      qDebug()<<"no session in list";
    }
}

void ListControl::sendMessageByFile(QString sessionid, int type, QString localPath, QString fileName)
{
  //判断是否可以发送
  if(IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->count(sessionid)>0){

      // 执行发送操作
      //1 将 消息 拼接到 列表
      QDateTime time = QDateTime::currentDateTime();   //获取当前时间
      int timeT = time.toTime_t();   //将当前时间转为时间戳
      int64_t ts=timeT;
      int msec=time.time().msec();
      ts=ts*1000+msec;
      UIMessageChatModel *message=new UIMessageChatModel(sessionid,"aaa","qrc:/other/icon/qqimg.jpg",ts,"dad",2);
      message->setBody("[file]"+fileName);
      IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->find(sessionid).value()->addMessageToList(message);
      // 更新
      Init::Instance()->getEngine()->rootContext()->setContextProperty("currSessionList",QVariant::fromValue(IM_GUI_KIT::IM_CoreApi::Instance()->getListControl()->getMessageMapPort()->find(sessionid).value()->getMessageList()));
//      emit recvMessageEven("session","name","time","summary");
    }
  else {
      qDebug()<<"no session in list";
    }
}

int ListControl::getSessionSize()
{
  return IM_CoreApi::Instance()->getListControl()->getSessionListPort()->size();
}

int ListControl::getFriendSize()
{
    return IM_CoreApi::Instance()->getListControl()->getFriendListPort()->size();
}

ListControl::ListControl()
{
  qDebug()<<"init ListControl";
  // 注册 对象到 全局
  IM_CoreApi::Instance()->setListApiPort(this);
}
